package com.szzg10.thinking.chapter18;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;

public class Logon implements Serializable {

	public static void main(String[] args) throws IOException, ClassNotFoundException {
		Logon logon = new Logon("Peter", "123456");
		System.out.println(logon);
		System.out.println();
		
		//write
		File file = new File(Worm.FILE_NAME_SERIALIZABLE);
		FileOutputStream fileOutputStream = new FileOutputStream(file);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
		objectOutputStream.writeObject(logon);
		objectOutputStream.close();
		
		//read
		FileInputStream fileInputStream = new FileInputStream(file);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		logon = (Logon)objectInputStream.readObject();
		System.out.println(logon);
		objectInputStream.close();
	}
	
	private String name;
	private transient String password;
	private Date date = new Date();
	
	public Logon(String name, String password) {
		this.name = name;
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "name: " + name + ", password: " + password + ", date: " + date;
	}
}
